package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/1/18 0018.
 */
@Service
public class UserServiceImpl {
    private Logger logger= LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserRepository userRepository;

    public void save(User p){
        //myAgentRepository.save()
        userRepository.save(p);
        logger.info("-----userservice---save success-------------");
    }

    public void del(Long id){
        User user= (User) userRepository.findOne(id);
        userRepository.delete(user);
    }

    public void update(User u){
        User user= (User) userRepository.findOne(u.getId());
        user.setName(u.getName());
        user.setPhone(u.getPhone());
        user.setDescription(u.getDescription());
        user.setAddress(u.getAddress());
        user.setCompany(u.getCompany());
        userRepository.saveAndFlush(user);
    }

    /*查询上级领导*/
    public User findLeaderByParentIdAsId(Long parentId){
        User user=userRepository.findLeaderByParentIdAsId( parentId);
        return user;
    }

    /*查询下级会员代理领导*/
    public List<User> findAgentByParentId(Long parentId){
        List<User> userList=userRepository.findAgentByParentId( parentId);
        return userList;
    }

    public User findById(Long id){
        User user=userRepository.findOne( id);
        return user;
    }

    public User findByProperty(String  str){
        User user=userRepository.findOneByName( str);
        return user;
    }

    public List<User> findAll(){
        List<User> userList=userRepository.findAll();
        return userList;
    }


}
